﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Core.MyLinq;
using Huirui.Cavan.Core.Persistent;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule
{
    public class UnPaySpecification : ISpecification<UnPay, UnPaySearch>
    {
        #region ISpecification<UnPay,UnPaySearch> 成员

        public UnPaySearch SearchParams
        {
            get;
            set;
        }

        #endregion

        #region ISpecification<UnPay> 成员

        public System.Linq.Expressions.Expression<Func<UnPay, bool>> Expression
        {
            get 
            {
                IQueryBuilder<UnPay> queryBuilder = QueryBuilder.Create<UnPay>();

                if (SearchParams.IsNotNull())
                {
                    queryBuilder = queryBuilder.Greater(up => up.LeftAmount, 0);
                    if (SearchParams.StartDate.HasValue && SearchParams.EndDate.HasValue)
                    {
                        queryBuilder = queryBuilder.Greater(up => up.CreateDate, SearchParams.StartDate.Value);
                        queryBuilder = queryBuilder.Less(up => up.CreateDate, SearchParams.EndDate.Value);
                    }
                    if (SearchParams.UnPayCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(up => up.UnPayCode, SearchParams.UnPayCode);
                    }
                    if (SearchParams.SupplierCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(up => up.Supplier.Id, SearchParams.SupplierCode);
                    }
                    if (SearchParams.UnPayType > 0)
                    {
                        queryBuilder = queryBuilder.Equals(up => (int)up.UnPayType, SearchParams.UnPayType);
                    }
                }

                return queryBuilder.Expression.Expand();
            }
        }

        #endregion
    }
}
